﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class users_user_register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(users_user_register));
        if (!this.IsPostBack){

            this.BtnOK.Attributes.Add("onclick", "return check_submit()");
            this.Txt_user_name.Attributes.Add("onchange", "javascript:onchanged_user_name()");
            this.Txt_email.Attributes.Add("onchange", "javascript:onchanged_email()");


            DataTable doc_type_dt = DataBaseOperate_SQLServer.GetDataTable("select doc_type_id,doc_name from t_doc_type");

            this.Ddl_doc_type_id.DataSource = doc_type_dt;
            this.Ddl_doc_type_id.DataValueField = "doc_type_id";
            this.Ddl_doc_type_id.DataTextField = "doc_name";
            this.Ddl_doc_type_id.DataBind();

        }

         
    }





    protected void BtnOK_Click(object sender, EventArgs e)
    {
        bool sql_success = false;

        if (!SysCommon.CheckCodeSame(this.Txt_CheckCode.Text.Trim()))
        {
            SysCommon.JavaScript.alert("校验码错误！请重新输入！");
            return;
        }

        SqlTransaction myTrans = DataBaseOperate_SQLServer.objConnection.BeginTransaction();

        try
        {
            if (SysCommon.ProcessSqlStr(this.Txt_user_name.Text).Length == 0)
            {
                SysCommon.JavaScript.alert("请输入用户名！");
                return;
            }

            if (SysCommon.ProcessSqlStr(this.Txt_user_password.Text).Length == 0)
            {
                SysCommon.JavaScript.alert("请输入密码！");
                return;
            }

            if (this.Txt_user_password.Text != this.Txt_user_password2.Text)
            {
                SysCommon.JavaScript.alert("两次密码输入不正确！");
                return;
            }
            
            SqlCommand objCommand = new SqlCommand();
            objCommand.Connection = DataBaseOperate_SQLServer.objConnection;
            objCommand.Transaction = myTrans;

            objCommand.CommandText = "select count(1) from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "'";

            if ((int)objCommand.ExecuteScalar() > 0)
            {
                SysCommon.JavaScript.alert("已有重名用户，请重新输入用户名！");

                myTrans.Rollback();

                return;
            }

            objCommand.CommandText = "select count(1) from t_user where email='" + SysCommon.ProcessSqlStr(this.Txt_email.Text) + "'";

            if ((int)objCommand.ExecuteScalar() > 0)
            {
                SysCommon.JavaScript.alert("该邮箱已注册，请重新输入邮箱地址！");

                myTrans.Rollback();

                return;
            }

            String[] arrField = { "user_name", "gender", "user_password", "email", "mobile", "true_name", "doc_type_id", "doc_num", "telephone", "address", "zip_code" };

            System.Text.StringBuilder sbstr = new System.Text.StringBuilder(200);
            sbstr.Append("insert into t_user(");
            for (int i = 0; i < arrField.Length; i++)
            {
                sbstr.Append(arrField[i]);
                if (i < arrField.Length - 1)
                {
                    sbstr.Append(",");
                }
            }
            sbstr.Append(") values (");
            for (int i = 0; i < arrField.Length; i++)
            {
                sbstr.Append("@");
                sbstr.Append(arrField[i]);
                if (i < arrField.Length - 1)
                {
                    sbstr.Append(",");
                }
            }

            sbstr.Append(")");
            objCommand.CommandText = sbstr.ToString();

            SqlParameter tempParameter;

            ContentPlaceHolder cph_Right;
            cph_Right = (ContentPlaceHolder)Master.FindControl("CPH_Right");

            for (int i = 0; i < arrField.Length; i++)
            {

                if ((cph_Right.FindControl("Txt_" + arrField[i]) is TextBox))
                {
                    tempParameter = new SqlParameter("@" + arrField[i], SqlDbType.VarChar);
                    tempParameter.Value = ((TextBox)(cph_Right.FindControl("Txt_" + arrField[i]))).Text;
                    objCommand.Parameters.Add(tempParameter);
                }
                else if ((cph_Right.FindControl("Ddl_" + arrField[i]) is DropDownList))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((DropDownList)(cph_Right.FindControl("Ddl_" + arrField[i]))).SelectedValue);
                }
                else if ((cph_Right.FindControl("Chk_" + arrField[i]) is CheckBox))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((CheckBox)(cph_Right.FindControl("Chk_" + arrField[i]))).Checked == true ? 1 : 0);
                }
                else
                {
                    objCommand.Parameters.Add("@" + arrField[i], "");
                }
            }
            //DataBaseOperate_SQLServer.ExecUpdate(objCommand);
            objCommand.ExecuteNonQuery();

            myTrans.Commit();

            sql_success = true;

        }
        catch (Exception ex)
        {
            Log.Record(ex);
            myTrans.Rollback();
        }

        if (sql_success == true)
        {
            DataRow dr = DataBaseOperate_SQLServer.GetDataRow("select * from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "'");

            Session["user_id"] = dr["user_id"];
            Session["user_name"] = dr["user_name"];
            Session["user_email"] = dr["email"];

            //SysCommon.JavaScript.alert("用户注册成功！");

            if (sql_success == true)
            {
                Response.Redirect("user_toIndex.aspx");
            }
        }

    }

    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public bool check_user_name(string temp_user_name,int name_or_email)
    {
        int find_user = -1;
        if (name_or_email == 0)
        {
            find_user = (int)DataBaseOperate_SQLServer.GetValue("select count(1) from t_user where user_name='" + SysCommon.ProcessSqlStr(temp_user_name) + "'");
        }
        else if (name_or_email == 1)
        {
            find_user = (int)DataBaseOperate_SQLServer.GetValue("select count(1) from t_user where email='" + SysCommon.ProcessSqlStr(temp_user_name) + "'");
        }

        if (find_user == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }



}
